﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.AspNet.SignalR.Client;
using Micua.iChat.Models;
using Micua.iChat.ViewModels;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍

namespace Micua.iChat.Views
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainViewModel ViewModel { get; set; }
        public MainPage()
        {
            ViewModel = new MainViewModel();
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ViewModel.CurrentUser = e.Parameter as User;
            App.Current.DataHubProxy.On<ObservableCollection<FriendGroup>>("GetFriendGroupCallback", async data =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    //ViewModel.Friends = new CollectionViewSource();
                    ViewModel.Friends.IsSourceGrouped = true;
                    ViewModel.Friends.ItemsPath = new PropertyPath("Friends");
                    ViewModel.Friends.Source = data;
                });
            });


            App.Current.AccountHubProxy.On<User>("NewFriendComeon", async user =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var list = ViewModel.Friends.Source as ObservableCollection<FriendGroup>;
                    var group = list.FirstOrDefault(g => g.GroupName == user.GroupName);
                    var user1 = group.Friends.FirstOrDefault(u => u.Username == user.Username);
                    user1.Status = UserStatus.Online;
                    //System.Diagnostics.Debug.WriteLine(group.Friends.Count);
                });
            });

            App.Current.DataHubProxy.Invoke("GetFriendGroup");
        }

        private void Flyout_Tapped(object sender, TappedRoutedEventArgs e)
        {
            FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
        }

        private void Pivot_PivotItemLoaded(Pivot sender, PivotItemEventArgs args)
        {
            if (args.Item.Name.Equals("dynamicPivotItem"))
            {
                functionSlideUp.Begin();
            }
        }

        private void ListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frame.Navigate(typeof(ChatPage));
        }
    }
}
